package com.store.view.basic;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.store.application.Customer;
import com.store.application.Order;
import com.store.application.Product;
import com.store.application.Store;
import com.store.view.SpringContext;

/**
 * Servlet implementation class GestionFormServlet
 */
public class EditCustomerProfileServlet extends HttpServlet {
	
	Store store;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditCustomerProfileServlet() {
        super();
        ApplicationContext context = SpringContext.getApplicationContext();
        store =  (Store)context.getBean("store");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String orderIdentifier = request.getParameter("orderIdentifier");
		Order order = store.getOrder(Integer.parseInt(orderIdentifier));
		request.setAttribute("order", order);
		request.getRequestDispatcher("/JSP/basic/editCustomerProfile.jsp").forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String orderIdentifier = request.getParameter("orderIdentifier");
		Order order = store.getOrder(Integer.parseInt(orderIdentifier));
		
		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");
		Customer customer = new Customer(firstName, lastName);
		
		try {
			int customerIdentifier = store.addCustomer(order, customer);
		} catch (Exception e) {
			new ServletException(e.getMessage());
		}
		
		response.sendRedirect("/venteEnLigne/index.html");			
	}

}
